package com.mc.moring.base

import android.content.Context
import android.content.SharedPreferences

class SharedPreferencesZiLiPageTool(context: Context, name: String?) {
    private val sp: SharedPreferences
    private val editor: SharedPreferences.Editor

    init {
        sp = context.getSharedPreferences(name, Context.MODE_PRIVATE)
        editor = sp.edit()
    }

    /**
     *
     * @param key key
     * @param value 值
     */
    fun put(key: String?, value: Any) {
        val type = value.javaClass.simpleName
        if ("String" == type) {
            editor.putString(key, value as String)
        } else if ("Integer" == type) {
            editor.putInt(key, (value as Int))
        } else if ("Boolean" == type) {
            editor.putBoolean(key, (value as Boolean))
        } else if ("Float" == type) {
            editor.putFloat(key, (value as Float))
        } else if ("Long" == type) {
            editor.putLong(key, (value as Long))
        }
        editor.commit()
    }


    fun putString(key: String?, value: String?): SharedPreferences.Editor {
        return editor.putString(key, value)
    }

    fun putInt(key: String?, value: Int): SharedPreferences.Editor {
        return editor.putInt(key, value)
    }

    fun putLong(key: String?, value: Long): SharedPreferences.Editor {
        return editor.putLong(key, value)
    }

    fun putFloat(key: String?, value: Float): SharedPreferences.Editor {
        return editor.putFloat(key, value)
    }

    fun putBoolean(key: String?, value: Boolean): SharedPreferences.Editor {
        return editor.putBoolean(key, value)
    }

    /**
     * @param key key
     * @param defaultValue 默认值
     * @return
     */
    operator fun get(key: String?, defaultValue: Any): Any? {
        val type = defaultValue.javaClass.simpleName
        if ("String" == type) {
            return sp.getString(key, defaultValue as String)
        } else if ("Integer" == type) {
            return sp.getInt(key, (defaultValue as Int))
        } else if ("Boolean" == type) {
            return sp.getBoolean(key, (defaultValue as Boolean))
        } else if ("Float" == type) {
            return sp.getFloat(key, (defaultValue as Float))
        } else if ("Long" == type) {
            return sp.getLong(key, (defaultValue as Long))
        }
        return null
    }


    fun remove(key: String?): SharedPreferences.Editor {
        return editor.remove(key)
    }


    fun clear(): SharedPreferences.Editor {
        return editor.clear()
    }


    fun commit(): Boolean {
        return editor.commit()
    }
}